package models

import (
	"rongyy/src/app"
	"rongyy/src/middlewares"
)

type MiniProgramToken struct {
	BaseModel `xorm:"extends"`
	Token     string `json:"token"`
	ExpiresIn int    `json:"expires_in"`
}

type ReqAccessToken struct {
	GrantType string `json:"grant_type"`
	Appid     string `json:"appid"`
	Secret    string `json:"secret"`
}

func (MiniProgramToken) TableName() string {
	return "mp_token"
}

func (mpt *MiniProgramToken) Creat() error {
	_, err := app.DB().Insert(mpt)
	if err != nil {
		middlewares.Logger.Println("MiniProgramToken Creat failed", err)
		return err
	}
	return nil
}

func (mpt *MiniProgramToken) First() error {
	_, err := app.DB().Where("id = ?", 1).Get(mpt)
	if err != nil {
		middlewares.Logger.Println("MiniProgramToken First failed", err)
		return err
	}
	return nil
}

func (mpt *MiniProgramToken) Update() error {
	_, err := app.DB().Where("token = 'expires'").Update(mpt)
	if err != nil {
		middlewares.Logger.Println("MiniProgramToken Update failed", err)
		return err
	}
	return nil
}

func (mpt *MiniProgramToken) GzhFirst() error {
	_, err := app.DB().Table("gzh_token").Where("id = ?", 1).Get(mpt)
	if err != nil {
		middlewares.Logger.Println("MiniProgramToken First failed", err)
		return err
	}
	return nil
}

func (mpt *MiniProgramToken) UpdateGzh() error {
	_, err := app.DB().Table("gzh_token").ID(1).Update(mpt)
	if err != nil {
		middlewares.Logger.Println("MiniProgramToken Update failed", err)
		return err
	}
	return nil
}
